using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Services;
using System;
using System.Web.UI.WebControls;
using UserInterface.Misc;

namespace UserInterface.Admin
{
    public partial class ManageForums : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCategoriesData();
            }
        }

        protected void NewCategoryButton_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("NewCategory.aspx");
        }

        protected void BindCategoriesData()
        {
            CategoryRepeater.DataSource = CategoryService.GetAll();
            CategoryRepeater.DataBind();
        }

        protected void CategoryRepeater_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton removeImageButton = (ImageButton)e.Item.FindControl("RemoveCategoryImageButton");
                string confirmMessage = (string)GetLocalResourceObject("ConfirmDeleteCategory");
                UITools.AddConfirmMessage(removeImageButton, confirmMessage);
            }
        }

        protected void CategoryRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            byte categoryId = byte.Parse((string)e.CommandArgument);
            Category category = CategoryService.GetById(categoryId);
            if (category != null)
            {
                if (e.CommandName == "Edit")
                {
                    Server.Transfer("EditCategory.aspx?categoryId=" + category.Id);
                }
                else if (e.CommandName == "NewForum")
                {
                    Server.Transfer("NewForum.aspx?categoryId=" + category.Id);
                }
                else if (e.CommandName == "Up")
                {
                    CategoryService.MoveCategoryUp(category);
                }
                else if (e.CommandName == "Down")
                {
                    CategoryService.MoveCategoryDown(category);
                }
                else if (e.CommandName == "Remove")
                {
                    CategoryService.RemoveCategory(category);
                }
                BindCategoriesData();
            }
        }

        protected void ForumRepeater_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton removeImageButton = (ImageButton)e.Item.FindControl("RemoveForumImageButton");
                string confirmMessage = (string)GetLocalResourceObject("ConfirmDeleteForum");
                UITools.AddConfirmMessage(removeImageButton, confirmMessage);
            }
        }

        protected void ForumRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            short forumId = short.Parse((string)e.CommandArgument);
            Forum forum = ForumService.GetById(forumId);
            if (forum != null)
            {
                if (e.CommandName == "Edit")
                {
                    Server.Transfer("EditForum.aspx?forumId=" + forum.Id);
                }
                else if (e.CommandName == "Up")
                {
                    ForumService.MoveForumUp(forum);
                }
                else if (e.CommandName == "Down")
                {
                    ForumService.MoveForumDown(forum);
                }
                else if (e.CommandName == "Remove")
                {
                    ForumService.RemoveForum(forum);
                }
                BindCategoriesData();
            }
        }
    }
}
